package com.ruoyi.property.task;

import com.ruoyi.property.constant.propertyConstants;
import com.ruoyi.system.domain.GmTenantInfo;
import com.ruoyi.system.domain.ParkRate;
import com.ruoyi.system.domain.TenantInfo;
import com.ruoyi.system.service.IGmParkRateService;
import com.ruoyi.system.service.IGmTenantInfoService;
import com.ruoyi.system.service.ITenantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * 状态校验
 * @author SuXin
 * @version 1.0 2021/9/2
 */
@Component("timedTask")
public class TimedTask {

    @Autowired
    private ITenantInfoService tenantInfoService;

    @Autowired
    private IGmTenantInfoService gmTenantInfoService;

    @Autowired
    private IGmParkRateService gmParkRateService;

    @Scheduled(cron="0 0/15 1 * * ?")
    public void checkTenantEndTime(){

        Date now = new Date();
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setTenantStatus(propertyConstants.NOMAL);
        List<TenantInfo> list = tenantInfoService.selectTenantInfoList(tenantInfo);
        tenantInfo.setTenantStatus(propertyConstants.LINQI);
        List<TenantInfo> list2 = tenantInfoService.selectTenantInfoList(tenantInfo);
        list.addAll(list2);
        if (list!=null&&!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                long  days = (now.getTime() - list.get(i).getTenantEndTime().getTime()) / (1000 * 3600 * 24);
                if(days>=0){
                    list.get(i).setTenantStatus(propertyConstants.DAOQI);
                    list.get(i).setRemark("租赁到期,已过期"+days+"天");
                    tenantInfoService.updateTenantInfo(list.get(i));
                }else if(days>=-20&&days<0){
                    list.get(i).setTenantStatus(propertyConstants.LINQI);
                    list.get(i).setRemark("租赁临期,还差"+(1-days)+"天到期");
                    tenantInfoService.updateTenantInfo(list.get(i));
                }else {
                    list.get(i).setTenantStatus(propertyConstants.NOMAL);
                    tenantInfoService.updateTenantInfo(list.get(i));
                }
            }
        }
    }

//    "0 0/15 1 * * ?"每天凌晨1点每15分钟
//    国贸租户状态判定-只有正常的去判断是否到期
    @Scheduled(cron="0 0/15 1 * * ?")
    public void checkGMTenantEndTime(){

        Date now = new Date();
        GmTenantInfo gmTenantInfo = new GmTenantInfo();
//        查询所有正常-临期状态租户信息
        gmTenantInfo.setTenantStatus(propertyConstants.NOMAL);
        List<GmTenantInfo> list = gmTenantInfoService.selectGmTenantInfoList(gmTenantInfo);

        if (list!=null&&!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                long  days = (now.getTime() - list.get(i).getTenantEndTime().getTime()) / (1000 * 3600 * 24);
                if(days>=0){
                    list.get(i).setTenantStatus(propertyConstants.DAOQI);
                    //list.get(i).setRemark("租赁到期,已过期"+days+"天");
                    gmTenantInfoService.updateGmTenantInfo(list.get(i));
                }else {
                    list.get(i).setTenantStatus(propertyConstants.NOMAL);
                    gmTenantInfoService.updateGmTenantInfo(list.get(i));
                }
            }
        }
    }

    //    "0 0/15 1 * * ?"每天凌晨1点每15分钟
    @Scheduled(cron="0 0/15 1 * * ?")
    public void checkGMUpdateTime(){

        Date now = new Date();
        ParkRate parkRate = new ParkRate();
//        查询所有正常-临期状态租户信息
        parkRate.setStatus(propertyConstants.NOMAL);
        List<ParkRate> list = gmParkRateService.selectParkRateList(parkRate);
        parkRate.setStatus(propertyConstants.LINQI);
        List<ParkRate> list2 = gmParkRateService.selectParkRateList(parkRate);
        list.addAll(list2);
        if (list!=null&&!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                long  days = (now.getTime() - list.get(i).getUpdateTime().getTime()) / (1000 * 3600 * 24);
                if(days>=0){
                    list.get(i).setStatus(propertyConstants.LINQI);
                    //list.get(i).setRemark("到期");
                    gmParkRateService.updateParkRate(list.get(i));
                }else if(days<0){
                    list.get(i).setStatus(propertyConstants.NOMAL);
                    //list.get(i).setRemark("正常");
                    gmParkRateService.updateParkRate(list.get(i));
                }else {
                    list.get(i).setStatus(propertyConstants.DAOQI);//退租
                    gmParkRateService.updateParkRate(list.get(i));
                }
            }
        }
    }
}
